package com.hcinfos.gsoo.adapter;

import java.util.List;

import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.GdMealsDish;
import com.hcinfos.gsoo.interfaze.DataCallback;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author xi 已点的菜单
 * 
 */
public class HaveOrderAdapter extends BaseAdapter {

	private List<GdMealsDish> list;
	private LayoutInflater mInflater;
	public int index = 0;
	public Context context;
	public boolean isVip = false;
	private int position = 0;
	public DataCallback call;

	public HaveOrderAdapter(Context context, List<GdMealsDish> list) {
		this.list = list;
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Holder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.adapter_have_order_view,
					null);
			holder = new Holder();
			holder.menu_detail_name_tv = (TextView) convertView
					.findViewById(R.id.menu_detail_name_tv);
			holder.add_ib = (ImageButton) convertView.findViewById(R.id.add_ib);
			holder.sub_ib = (ImageButton) convertView.findViewById(R.id.sub_ib);
			holder.menuDetail_iv = (ImageView) convertView
					.findViewById(R.id.menuDetail_iv);
			holder.price_tv = (TextView) convertView
					.findViewById(R.id.price_tv);
			holder.operator_rl = convertView.findViewById(R.id.operator_rl);
			holder.detail_num_tv = (TextView) convertView
					.findViewById(R.id.detail_num_tv);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final GdMealsDish dish = list.get(position);
		holder.menu_detail_name_tv.setText(dish.getDishName());
		holder.price_tv.setText("¥" + dish.getDishPrice());
		holder.detail_num_tv.setText(dish.getNum() + "");
		holder.add_ib.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				GdMealsDish extend = GsooApplication.menuMap.get(dish.getId());
				int num = extend.getNum();
				num += 1;
				extend.setNum(num);
				holder.detail_num_tv.setText(num + "");
				if (call != null)
					call.callback(0);
			}
		});
		holder.sub_ib.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				GdMealsDish extend = GsooApplication.menuMap.get(dish.getId());
				if (extend != null) {
					int num = extend.getNum();
					num -= 1;
					if (num <= 0) {
						num = 0;
						GsooApplication.menuMap.remove(dish.getId());
					}
					extend.setNum(num);
					holder.detail_num_tv.setText(num + "");
				}
				if (call != null)
					call.callback(0);
			}
		});
		return convertView;
	}

	private final class Holder {
		TextView menu_detail_name_tv;
		ImageButton add_ib;
		ImageButton sub_ib;
		ImageView menuDetail_iv;
		TextView price_tv;
		TextView detail_num_tv;
		View operator_rl;

	}

	public void setSelect(int position) {

		this.position = position;
	}

	public void setDataChange(List<GdMealsDish> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void setDataCallback(DataCallback callback) {
		this.call = callback;
	}

}
